package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Crawlerfilecolumn;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("CrawlerfilecolumnController")
public class CrawlerfilecolumnController extends CmiEmaController {

	@RequestMapping("/listCrawlerfilecolumns")
	public ModelAndView listCrawlerfilecolumns(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerfilecolumnPaginationService.updatePagination(request, pagination);
		Set<Crawlerfilecolumn> crawlerfilecolumns = crawlerfilecolumnPaginationService
				.getPaginationCrawlerfilecolumns(pagination);

		mav.addObject("crawlerfilecolumns", crawlerfilecolumns);
		mav.addObject("entityName", "crawlerfilecolumn");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerfilecolumn/listCrawlerfilecolumns");
		return mav;
	}

	public String listCrawlerfilecolumns() {
		return "redirect:/listCrawlerfilecolumns";
	}

	@RequestMapping("/viewCrawlerfilecolumn")
	public ModelAndView viewCrawlerfilecolumn(@RequestParam Integer crawlerfilecolumnId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerfilecolumn", crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnId));
		mav.setViewName("crawlerfilecolumn/viewCrawlerfilecolumn");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerfilecolumns")
	public ModelAndView confirmDeleteCrawlerfilecolumns(
			@RequestParam(value = "deleteCrawlerfilecolumnsIds", required = true) Set<Integer> deleteCrawlerfilecolumnsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerfilecolumn> deleteCrawlerfilecolumns = crawlerfilecolumnDAO
				.findCrawlerfilecolumnsByIds(deleteCrawlerfilecolumnsIds);
		Set<Crawlerfilecolumn> referencedCrawlerfilecolumns = crawlerfilecolumnService
				.loadReferenceCrawlerfilecolumns(deleteCrawlerfilecolumns);
		boolean hasReferenced;
		if (referencedCrawlerfilecolumns.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerfilecolumns);
			hasReferenced = false;
		} else {
			deleteCrawlerfilecolumns.removeAll(referencedCrawlerfilecolumns);
			mav.addObject("referencedEntitys", referencedCrawlerfilecolumns);
			mav.addObject("deleteEntitys", deleteCrawlerfilecolumns);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerfilecolumn");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerfilecolumns");
		return mav;
	}

	@RequestMapping("/deleteCrawlerfilecolumn")
	public String deleteCrawlerfilecolumn(@RequestParam Integer crawlerfilecolumnId) {
		Crawlerfilecolumn crawlerfilecolumn = crawlerfilecolumnDAO
				.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnId);
		crawlerfilecolumnService.deleteCrawlerfilecolumn(crawlerfilecolumn);
		return "forward:/listCrawlerfilecolumns";
	}

	@RequestMapping("/confirmDeleteCrawlerfilecolumn")
	public ModelAndView confirmDeleteCrawlerfilecolumn(@RequestParam Integer crawlerfilecolumnId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerfilecolumn", crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnId));

		mav.setViewName("crawlerfilecolumn/deleteCrawlerfilecolumn");

		return mav;
	}

	@RequestMapping("/deleteCrawlerfilecolumns")
	public String deleteCrawlerfilecolumns(
			@RequestParam(value = "deleteCrawlerfilecolumnsId", required = true) Set<Integer> deleteCrawlerfilecolumnsId) {
		crawlerfilecolumnService.deleteCrawlerfilecolumns(deleteCrawlerfilecolumnsId);
		return "forward:/listCrawlerfilecolumns";
	}

	@RequestMapping("/editCrawlerfilecolumn")
	public ModelAndView editCrawlerfilecolumn(@RequestParam Integer crawlerfilecolumnId) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilecolumn crawlerfilecolumn = crawlerfilecolumnDAO
				.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerfilecolumn", crawlerfilecolumn);

		// 把所有的引用crawlerfilecolumn对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilecolumnService.loadCrawlerfilecolumnObjectReferencedSet(crawlerfilecolumn));

		// 将编辑页面crawlerfilecolumn添加到mav中
		mav.setViewName("crawlerfilecolumn/editCrawlerfilecolumn");
		return mav;
	}

	@RequestMapping("/newCrawlerfilecolumn")
	public ModelAndView newCrawlerfilecolumn() {
		ModelAndView mav = new ModelAndView();
		Crawlerfilecolumn crawlerfilecolumn = new Crawlerfilecolumn();

		mav.addObject("crawlerfilecolumn", crawlerfilecolumn);

		// 把所有的引用crawlerfilecolumn对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilecolumnService.loadCrawlerfilecolumnObjectReferencedSet(crawlerfilecolumn));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerfilecolumn/editCrawlerfilecolumn");

		return mav;
	}

	@RequestMapping("/saveCrawlerfilecolumn")
	public ModelAndView saveCrawlerfilecolumn(@ModelAttribute Crawlerfilecolumn crawlerfilecolumn) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilecolumn existingCrawlerfilecolumn = CmiHibernateProxyUtil
				.deproxy(crawlerfilecolumnDAO.findCrawlerfilecolumnByPhysicalPrimaryKey(crawlerfilecolumn.getName()));

		// 新建记录
		if (crawlerfilecolumn.getId() == null) {
			if (crawlerfilecolumn.equals(existingCrawlerfilecolumn)) {

				mav.addAllObjects(crawlerfilecolumnService.loadCrawlerfilecolumnClassReferencedSet(crawlerfilecolumn));
				// mav = editContinue(mav, crawlerfilecolumn);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilecolumn/editCrawlerfilecolumn");
			} else {
				crawlerfilecolumnService.saveCrawlerfilecolumn(crawlerfilecolumn);
				mav.setViewName("forward:/listCrawlerfilecolumns");
			}
		}
		// 编辑记录
		else {
			Crawlerfilecolumn originCrawlerfilecolumn = CmiHibernateProxyUtil
					.deproxy(crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumn.getId()));
			if (!crawlerfilecolumn.equals(originCrawlerfilecolumn)
					&& crawlerfilecolumn.equals(existingCrawlerfilecolumn)) {
				mav.addAllObjects(crawlerfilecolumnService.loadCrawlerfilecolumnClassReferencedSet(crawlerfilecolumn));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilecolumn/editCrawlerfilecolumn");
			} else {
				crawlerfilecolumnService.saveCrawlerfilecolumn(crawlerfilecolumn);
				mav.setViewName("forward:/listCrawlerfilecolumns");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerfilecolumnController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
